package com.atyjh.reggie.controller;

import com.atyjh.reggie.dto.Result;
import com.atyjh.reggie.dto.UserDTO;
import com.atyjh.reggie.entity.Employee;
import com.atyjh.reggie.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author yjh
 * @since jdk1.8.0_311 2022.06.07
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @PostMapping("/login")
    public Result<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        return employeeService.login(
                request.getSession(),
                employee.getUsername(),
                employee.getPassword());
    }

    @PostMapping("/logout")
    public Result<UserDTO> logout(HttpServletRequest request) {
        return employeeService.logout(request.getSession());
    }

    @PostMapping
    public Result<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        return employeeService.saveEmployee(request, employee);
    }

    @GetMapping("/page")
    public Result<Page<Employee>> page(@RequestParam("page")Integer page,
                                       @RequestParam("pageSize")Integer pageSize,
                                       @RequestParam(value = "name", required = false)String name) {

        return employeeService.pageEmployee(page, pageSize, name);

    }

    @PutMapping
    public Result<String> update(@RequestBody Employee employee, HttpServletRequest request) {
        return employeeService.update(request, employee);
    }

    @GetMapping("/{id}")
    public Result<Employee> getEmployee(@PathVariable("id")Long id) {
        return Result.success(employeeService.getById(id));
    }

}












